package com.px.baolaiapi.controller;


import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSONObject;
import com.px.common.api.CommonResult;
import com.px.mbg.model.DeviceCapacity;
import com.px.baolaiapi.ExcelDto.ExcelDeviceCapactyBaseDataDto;
import com.px.baolaiapi.ExcelDto.ExcelDeviceCapactyBomDataDto;
import com.px.baolaiapi.ExcelDto.ExcelDeviceCapactyDeviceDataDto;
import com.px.baolaiapi.page.DeviceCapacityGroupByBomPage;
import com.px.baolaiapi.page.DeviceCapacityGroupByDevicePage;
import com.px.baolaiapi.page.DeviceCapacityPage;
import com.px.baolaiapi.service.IDeviceCapacityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 产能占用信息 前端控制器
 * </p>
 *
 * @author 品讯科技
 * @since 2022-05-27
 */
@Api(tags = "设备产能占用信息Api")
@RestController
@RequestMapping("/device-capacity")
public class DeviceCapacityController {
    @Autowired
    private IDeviceCapacityService deviceCapacityService;

    @ApiOperation("查询设备占用产能-基本数据")
    @PostMapping("/queryPageBaseData")
    public CommonResult queryPageBaseData(@RequestBody DeviceCapacityPage deviceCapacityPage){
        return CommonResult.success(deviceCapacityService.queryPage(deviceCapacityPage));
    }

    @ApiOperation("设备产能分析-导出基本数据")
    @GetMapping("/downloadBaseData")
    public void downloadBaseData(HttpServletResponse response) throws IOException {
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        try {


            String fname = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")) + "设备产能基本信息";
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode(fname, "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            // 这里需要设置不关闭流

            List<DeviceCapacity> deviceCapacityList = deviceCapacityService.list();
            List<ExcelDeviceCapactyBaseDataDto> excelDeviceCapactyBaseDataDtoList = new ArrayList<>();
            deviceCapacityList.forEach( deviceCapacity -> {
                ExcelDeviceCapactyBaseDataDto excelDeviceCapactyBaseDataDto = new ExcelDeviceCapactyBaseDataDto();
                BeanUtils.copyProperties(deviceCapacity,excelDeviceCapactyBaseDataDto);
                excelDeviceCapactyBaseDataDtoList.add(excelDeviceCapactyBaseDataDto);
            });

            EasyExcel.write(response.getOutputStream(), ExcelDeviceCapactyBaseDataDto.class).sheet("数据列表").doWrite(excelDeviceCapactyBaseDataDtoList);

        } catch (Exception e) {
            // 重置response
            response.reset();
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            response.getWriter().println(JSONObject.parseObject(CommonResult.failed("下载文件失败" + e.getMessage()).toString()));
        }
    }

    @ApiOperation("通过设备维度-查询占用信息")
    @PostMapping("/queryDeviceCapacityGroupByDevice")
    public CommonResult queryDeviceCapacityGroupByDevice(@RequestBody DeviceCapacityGroupByDevicePage param){
        return CommonResult.success(deviceCapacityService.queryDeviceCapacityGroupByDevice(param));
    }

    @ApiOperation("设备产能分析-导出设备占用数据")
    @GetMapping("/downloadDeviceData")
    public void downloadDeviceData(HttpServletResponse response) throws IOException {
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        try {

            String fname = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")) + "设备占用信息";
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode(fname, "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            // 这里需要设置不关闭流

            List<DeviceCapacity> deviceCapacityList = deviceCapacityService.queryDeviceCapacityGroupByDeviceDownLoad();
            List<ExcelDeviceCapactyDeviceDataDto> excelDeviceCapactyDeviceDataDtos = new ArrayList<>();
            deviceCapacityList.forEach( deviceCapacity -> {
                ExcelDeviceCapactyDeviceDataDto excelDeviceCapactyDeviceDataDto = new ExcelDeviceCapactyDeviceDataDto();
                BeanUtils.copyProperties(deviceCapacity,excelDeviceCapactyDeviceDataDto);
                excelDeviceCapactyDeviceDataDtos.add(excelDeviceCapactyDeviceDataDto);
            });

            EasyExcel.write(response.getOutputStream(), ExcelDeviceCapactyDeviceDataDto.class).sheet("数据列表").doWrite(excelDeviceCapactyDeviceDataDtos);

        } catch (Exception e) {
            // 重置response
            response.reset();
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            response.getWriter().println(JSONObject.parseObject(CommonResult.failed("下载文件失败" + e.getMessage()).toString()));
        }
    }

    @ApiOperation("通过Bom维度-查询占用信息")
    @PostMapping("/queryDeviceCapacityGroupByBom")
    public CommonResult queryDeviceCapacityGroupByBom(@RequestBody DeviceCapacityGroupByBomPage param){
        return CommonResult.success(deviceCapacityService.queryDeviceCapacityGroupByBom(param));
    }


    @ApiOperation("设备产能分析-导出BOM占用信息")
    @GetMapping("/downloadBomData")
    public void downloadBomData(HttpServletResponse response) throws IOException {
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        try {


            String fname = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")) + "BOM占用信息";
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode(fname, "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            // 这里需要设置不关闭流

            List<DeviceCapacity> deviceCapacityList = deviceCapacityService.queryDeviceCapacityGroupByBomDownLoad();
            List<ExcelDeviceCapactyBomDataDto> excelDeviceCapactyBomDataDtos = new ArrayList<>();
            deviceCapacityList.forEach( deviceCapacity -> {
                ExcelDeviceCapactyBomDataDto excelDeviceCapactyBomDataDto = new ExcelDeviceCapactyBomDataDto();
                BeanUtils.copyProperties(deviceCapacity,excelDeviceCapactyBomDataDto);
                excelDeviceCapactyBomDataDtos.add(excelDeviceCapactyBomDataDto);
            });

            EasyExcel.write(response.getOutputStream(), ExcelDeviceCapactyBomDataDto.class).sheet("数据列表").doWrite(excelDeviceCapactyBomDataDtos);

        } catch (Exception e) {
            // 重置response
            response.reset();
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            response.getWriter().println(JSONObject.parseObject(CommonResult.failed("下载文件失败" + e.getMessage()).toString()));
        }
    }


}
